package com.sun.showcase.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class HomeController {
//	@Autowired
//	private ShopGoodsSearchService shopGoodsSearchService;
	@Autowired
//	private RedisSingleService redisSingleService;
//	@Autowired
//	private MessageProducerService messageProducerService;
//	@Autowired
//	private FastdfsService fastdfsService;
	@RequestMapping(value = {"/","/home"})
	public String home(){
		return "home_frontend";
	}
	@RequestMapping(value = {"/frontend"})
	public String frontend(){
		return "home_frontend";
	}
	@RequestMapping(value = {"/backend"})
	public String backend(){
		return "home_new";
	}
	@RequestMapping(value = "/noAuth",method = RequestMethod.GET)
	public String noAuth(){
		return "noAuth";
	}
	@RequestMapping(value = "/404",method = RequestMethod.GET)
	public String pageNotFound(){
		return "404";
	}
	@RequestMapping(value = "/500",method = RequestMethod.GET)
	public String serverError(){
		return "500";
	}
	@RequestMapping(value = "/goueditor",method = RequestMethod.GET)
	public String goueditor(){
		return "ueditor";
	}
	@RequestMapping(value = "/test",method = RequestMethod.GET)
	public String test(){
		return "test";
	}
	@RequestMapping(value = "/ueditor",method = RequestMethod.GET)
	public String ueditor(){
		return "ueditor";
	}
	//异常处理
	@RequestMapping("/error/{code}")
	public String noAccess(@PathVariable String code){
		
	    return "error/" + code;
	}
	
	//请求超时
	@RequestMapping("/timeout")
	public void timeout(@RequestParam(value = "id", required = false) String id,HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.addHeader("sessionstatus", "timeout");
		//ajax 超时处理
		if(request.getHeader("x-requested-with") !=null 
				&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){  
			
           response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
     
        }else{
        	
             response.sendRedirect(request.getContextPath()+"/login");  
        }
	}
}
